{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gensim\n",
    "import csv\n",
    "import string\n",
    "import re\n",
    "from collections import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(r'test.csv', 'r', encoding='utf-8') as reader:\n",
    "    data_list = [item for item in csv.reader(reader)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [],
   "source": [
    "def remove_urls(text):\n",
    "    text = re.sub(r'(https|http)?:\\/\\/(\\w|\\.|\\/|\\?|\\=|\\&|\\%)*\\b', '', text, flags=re.MULTILINE)\n",
    "    return text\n",
    "\n",
    "def remove_blank(text):\n",
    "    return re.sub(r'\\s+', ' ', text)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['4', '3', 'Mon May 11 03:17:40 UTC 2009', 'kindle2', 'tpryan', '@stellargirl I loooooooovvvvvveee my Kindle2. Not that the DX is cool, but the 2 is fantastic in its own right.']\n"
     ]
    }
   ],
   "source": [
    "print(data_list[0])\n",
    "# w2v = gensim.models.KeyedVectors.load_word2vec_format('data/GoogleNews-vectors-negative300.bin', binary=True)\n",
    "# print(w2v['BD'][:10])\n",
    "# print(w2v['bd'][:10])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "359\n"
     ]
    }
   ],
   "source": [
    "processed_data = []\n",
    "uid_dic = {}\n",
    "uid_cnt = []\n",
    "for item in data_list:\n",
    "    if int(item[0]) == 2:\n",
    "        continue\n",
    "    score_ = int(int(item[0]) / 4)\n",
    "    uid_ = item[4]\n",
    "    if uid_ in uid_dic.keys():\n",
    "        idx = uid_dic[uid_]\n",
    "    else:\n",
    "        idx = len(uid_dic)\n",
    "        uid_dic[uid_] = idx\n",
    "    uid_cnt.append(idx)\n",
    "    content_ = item[5]\n",
    "    # content_ = content_.lower()\n",
    "    content_ = remove_urls(content_)\n",
    "    remove = str.maketrans('', '', string.punctuation)\n",
    "    content_ = content_.translate(remove)\n",
    "    content_ = remove_blank(content_)\n",
    "    content_ = content_.strip()\n",
    "    processed_data.append([idx, content_, score_])\n",
    "\n",
    "# uid_cnt = Counter(uid_cnt)\n",
    "# processed_data = [item for item in processed_data if uid_cnt[item[0]] > 5]\n",
    "print(len(processed_data))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [],
   "source": [
    "import json\n",
    "with open('eval_processed.json', 'w') as writer:\n",
    "    json.dump(processed_data, writer)"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch1.10",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
